using B.S.RbacData.ErrorCode;
using ContentmanagementBaseRead.API.Applications.Commands.Questions;
using ContentmanagementBaseRead.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseRead.API.Controllers
{
    /// <summary>
    /// 问题管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class QuestionsController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<QuestionsController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public QuestionsController(IMediator mediator, ILogger<QuestionsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取常见问题分类枚举信息
        /// </summary>
        /// <returns>常见问题分类列表</returns>
        [HttpGet("categories")]
        public async Task<ApiResult<List<FAQCategoryDto>>> GetFAQCategories()
        {
            try
            {
                _logger.LogInformation("接收到获取常见问题分类枚举请求");

                GetFAQCategoriesCommand command = new GetFAQCategoriesCommand();
                var result = await _mediator.Send(command);

                _logger.LogInformation("常见问题分类枚举获取请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取常见问题分类枚举请求处理异常");
                return new ApiResult<List<FAQCategoryDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取常见问题分类枚举异常：{ex.Message}",
                    data = new List<FAQCategoryDto>()
                };
            }
        }

        /// <summary>
        /// 获取常见问题列表
        /// </summary>
        /// <param name="command">查询命令</param>
        /// <returns>常见问题列表</returns>
        [HttpGet("list")]
        public async Task<ApiPaging<List<ProblemListDto>>> GetProblemList([FromQuery] GetProblemListCommand command)
        {
            try
            {
                _logger.LogInformation("接收到获取常见问题列表请求，页码：{PageIndex}，每页数量：{PageSize}", 
                    command.PageIndex, command.PageSize);

                var result = await _mediator.Send(command);

                _logger.LogInformation("常见问题列表获取请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取常见问题列表请求处理异常");
                return new ApiPaging<List<ProblemListDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取常见问题列表异常：{ex.Message}",
                    data = new List<ProblemListDto>(),
                    TotalCount = 0,
                    PageCount = 0,
                   
                };
            }
        }
    }
}